<?php

namespace app\crontab\task;

// use app\common\model\vip\VipOrder;

use think\Db;
use app\common\model\ads\Ad;
use app\common\model\ads\Order as AdOrder;
use app\common\model\ads\GoldLog;

/**
 * 广告订单
 *
 * @icon fa fa-circle-o
 */
class Order extends Common
{
    public function __construct(array $params = [])
    {
        parent::__construct($params);
    }

    /**
     * 定时退款已经支付但是广告被拒绝的
     */
    public function refundAdOrder(){
        $list = Ad::alias('ad')
        ->join('__SELF_AD_ORDER__ o','ad.id = o.ad_id')
        ->field("ad.id as ad_id,ad.status as ad_status,o.id as order_id,o.status as pay_status,o.user_id,o.order_no,o.third_order_no,o.real_amount,o.paytime")
        ->where("ad.status",2)
        ->where("o.status",1)
        ->select();

        foreach ($list as $key => $ado) {
            $order = AdOrder::get($ado['order_id']);
            #执行退款
            try {
                $refund = \app\common\service\Payment::refundAdOrder($order);
            } catch (\Exception $e) {
                echo "注意：{$order->id}退款失败---".$e->getMessage()."\n";
                continue;
            }

            if(!$refund){
                echo "{$order->id}退款失败\n";
            }else{
                echo "{$order->id}退款{$order->real_amount}成功\n";
            }
        }

        return 0;
    }


    /**
     * 定时退款已经过期的赏金
     */
    public function refundAdGold(){
        $now = time();
        $list = Ad::alias('ad')
        ->join('__SELF_AD_ORDER__ o','ad.id = o.ad_id')
        ->field("ad.id as ad_id,ad.effecttime,ad.get_num,ad.gold_num,ad.status as ad_status,o.id as order_id,o.status as pay_status,o.user_id,o.order_no,o.third_order_no,o.real_amount,o.paytime,o.money_amount,o.service_fee")
        ->where("ad.status",1)
        ->where("ad.get_num < `ad`.`gold_num`")
        ->where("ad.effecttime",'<', $now)
        ->where("o.status",1)
        ->select();

        foreach ($list as $key => $ado) {
            $order = AdOrder::get($ado['order_id']);
            #执行退款
            try {
                #退款金额
                $refundMoney = bcmul(($ado['gold_num'] - $ado['get_num']) / $ado['gold_num'], $ado['money_amount'] - $ado['service_fee'], 2);
                $refund = \app\common\service\Payment::refundAdOrder($order, $refundMoney);
            } catch (\Exception $e) {
                echo "注意：{$order->id}退款失败---".$e->getMessage()."\n";
                continue;
            }

            if(!$refund){
                echo "{$order->id}退款失败\n";
            }else{
                echo "{$order->id}退款{$refundMoney}成功\n";
                $del = GoldLog::where('ad_id', $ado['ad_id'])->where('user_id',0)->delete();

                echo "成功删除{$del}个没人领的红包\n";
            }
        }

        return 0;
    }


    /**
     * 定时退款已经过期没人领的赏金红包
     */
    public function delAdGoldLog(){
        $now = time();
        $list = Ad::where("type",1)
        ->where("effecttime",'<', $now)
        ->where("get_num < `gold_num`")
        ->select();

        foreach ($list as $key => $ad) {
            $del = GoldLog::where('ad_id', $ad['id'])->where('user_id',0)->delete();

            echo "成功删除{$del}个红包\n";
        }

        return 0;
    }

}
